import { io } from "socket.io-client";
import { Connector } from "./ISocketComp";
import { STATE_EVENT } from "../State/State";

export function connect(
  this: Connector,
  name = "baize_client",
  url = "http://localhost:3000",
  options = {}
) {
  const socket = io(url, {
    timeout: 2000,
  });
  this.client = socket;
  socket.connect();

  socket.on("connect", () => {
    socket.emit("join", name);
    (this as Connector).system.state.pub(STATE_EVENT.CONNECT, true);
  });

  socket.on("disconnect", () => {
    console.log("断开连接");
    (this as Connector).system.state.pub(STATE_EVENT.DISCONNECT, true);
  });
}
